import * as THREE from 'three';
import TWEEN from '../../../node_modules/three/examples/jsm/libs/tween.module.js';

//绘制点群
class showPoint{
    constructor(scene,points,colors,pointSize){
        this.scene = scene;
        this.points = points;
        this.pointSize = pointSize;
        this.colors = colors;
        this.opacity={v:0}; // 初始透明度为0
        this.init();
        return 0;
    }
    createPoint(){
        const geometry = new THREE.BufferGeometry();
        geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( this.points, 3 ) );
        geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( this.colors, 3 ) );
        geometry.computeBoundingSphere();

        this.material = new THREE.PointsMaterial( { size: this.pointSize, vertexColors: true } );
        this.point_show = new THREE.Points( geometry, this.material );
        this.material.transparent=true;
        this.material.opacity=0;//刚加载时不显示，要显示时调用fadeIn
        this.scene.add( this.point_show );
    }
    fadeIn(callBack){
        new TWEEN.Tween(this.opacity)
        .to( {v:1} , 2000) // 2秒内透明度从0变为1
        .onUpdate(() => {
            this.material.opacity = this.opacity.v; // 更新材质透明度
        })
        .onComplete(() => {
          if(callBack){callBack();}
        })
        .start();
    }

    init(){
        this.createPoint();
    }
}

export default showPoint;